package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"zhhuiyan/models"
	. "zhhuiyan/authority"
	. "zhhuiyan/db"
	"image"
	"code.google.com/p/graphics-go/graphics"
	"github.com/disintegration/imaging"
	"strings"
	"regexp"
)

type ImageController struct {
	beego.Controller
}

func (this *ImageController) Set(tpl string) {
	this.Data["Path"] = "image"
	this.Layout = "layout.html"
	this.TplNames = "image/"+tpl
}
func (this *ImageController) Get() {
	id, _ := this.GetInt(":objectId")
	var model = &models.Image{Id:id, Object:&Object{Id:id}}
	if id != 0 {
		if err := Handler.Read(model); err != nil {
			fmt.Println(err)
		}
		if (model.Object != nil) {
			if err := Handler.Read(model.Object); err != nil {
				fmt.Println(err)
			}
		}
		tp := this.GetString("type")
		this.Data["Model"] = model
		if (tp != "") {
			this.Set(tp + ".html")
		}else {
			this.Set("show.html")
		}

		return
	}
	page := &models.Page{Size:20 }
	models := *new([]models.Image)
	if err := List("image", page, &models); err != nil {
		fmt.Println(err)
	}
	this.Data["Page"] = page
	this.Data["Models"] = models
	this.Set("list.html")
}
func (this *ImageController) Add() {
	this.Data["Model"] = &models.Image{ Object:&Object{}}
	this.Set("add.html")
}
func (this *ImageController) Post() {


	file, header, err := this.GetFile("image");
	defer file.Close();

	if (err != nil) {
		fmt.Println("file.get:", err)
	}
	var img image.Image;
	img, _, err = image.Decode(file)
	if (err != nil) {
		fmt.Println("image.Decode:", err)
	}
	dst := image.NewRGBA(image.Rect(0, 0, 40, 30))
	err = graphics.Scale(dst, img)
	if err != nil {
		fmt.Println("scale:", err)
	}
	re, _ := regexp.Compile("\\s")
	filename := re.ReplaceAllString(header.Filename, "-")
	err = imaging.Save(img, "upload/"+filename)
	if err != nil {
		fmt.Println("Save:", err)
	}
	thumbnail := strings.Split(header.Filename, ".");
	thumbname := ""
	length := len(thumbnail)
	for i := 0 ; i < length; i++ {
		if (i == 0) {
		}else if (i+1 == length) {
			thumbname+="_thumbnail."
		}else {
			thumbname+="."
		}
		thumbname+=thumbnail[i]
	}
	fmt.Println(filename)
	err = imaging.Save(dst, "upload/"+thumbname)
	if err != nil {
		fmt.Println("Save:", err)
	}
	object := &Object{Name: this.GetString("name")}
	if _, err := Handler.Insert(object) ; err != nil {
		fmt.Println("object:", err)
	}
	model := &models.Image{ Url:"/upload/" + filename,
		Thumbnail:"/upload/" + thumbname,
		Description: this.GetString("description"),
		Object:object}
	if _, err := Handler.Insert(model) ; err != nil {
		fmt.Println("image.persis:", err)
	}
	this.Data["Model"] = model
	this.Set("show.html")
}

func (this *ImageController) Edit() {


	file, header, err := this.GetFile("image");
	defer file.Close();

	if (err != nil) {
		fmt.Println("file.get:", err)
	}
	var img image.Image;
	img, _, err = image.Decode(file)
	if (err != nil) {
		fmt.Println("image.Decode:", err)
	}
	dst := image.NewRGBA(image.Rect(0, 0, 40, 30))
	err = graphics.Scale(dst, img)
	if err != nil {
		fmt.Println("scale:", err)
	}
	re, _ := regexp.Compile("\\s")
	filename := re.ReplaceAllString(header.Filename, "-")
	err = imaging.Save(img, "upload/"+filename)
	if err != nil {
		fmt.Println("Save:", err)
	}
	thumbnail := strings.Split(header.Filename, ".");
	thumbname := ""
	length := len(thumbnail)
	for i := 0 ; i < length; i++ {
		if (i == 0) {
		}else if (i+1 == length) {
			thumbname+="_thumbnail."
		}else {
			thumbname+="."
		}
		thumbname+=thumbnail[i]
	}
	fmt.Println(filename)
	err = imaging.Save(dst, "upload/"+thumbname)
	if err != nil {
		fmt.Println("Save:", err)
	}
	object := &Object{Name: this.GetString("name")}
	if _, err := Handler.Insert(object) ; err != nil {
		fmt.Println("object:", err)
	}
	model := &models.Image{ Url:"/upload/" + filename,
		Thumbnail:"/upload/" + thumbname,
		Description: this.GetString("description"),
		Object:object}
	if _, err := Handler.Insert(model) ; err != nil {
		fmt.Println("image.persis:", err)
	}
	this.Data["Model"] = model
	this.Set("show.html")
}

